package com.yanftch.stopcoding.request;

import com.alibaba.fastjson2.JSONObject;
import com.yanftch.stopcoding.bean.HotBean;
import com.yanftch.stopcoding.cb.RequestCallBack;
import com.yanftch.stopcoding.common.network.CallBack;
import com.yanftch.stopcoding.common.network.HttpUtils;
import com.yanftch.stopcoding.common.util.TextUtils;
import org.jetbrains.annotations.NotNull;
import com.yanftch.stopcoding.bean.TouTiao;

import java.util.ArrayList;

/**
 * 头条热榜
 */
public class TouTiaoHotRequest implements Request {

    private static final String TOUTIAO_URL = "https://is-lq.snssdk.com/api/suggest_words/?business_id=10016";

    public interface TouTiaoHotCallBack {
        void onSuccess(ArrayList<HotBean> list);

        void onError(Exception e);
    }

    @Override
    public void request(RequestCallBack callBack) {
        getToutiaoHotList(new TouTiaoHotCallBack() {
            @Override
            public void onSuccess(ArrayList<HotBean> list) {
                if (callBack != null)
                    callBack.onSuccess(list);
            }

            @Override
            public void onError(Exception e) {
                if (callBack != null)
                    callBack.onError(e);
            }
        });
    }

    private void getToutiaoHotList(@NotNull TouTiaoHotCallBack touTiaoHotCallBack) {
        HttpUtils.get(TOUTIAO_URL, new CallBack() {
            @Override
            public void onSuccess(String response) {
                if (TextUtils.isEmpty(response)) {
                    touTiaoHotCallBack.onError(new Exception("response is empty"));
                    return;
                }
                TouTiao touTiao = JSONObject.parseObject(response, TouTiao.class);
                ArrayList<HotBean> list = new ArrayList<>();
                if (touTiao != null && touTiao.getData() != null && !touTiao.getData().isEmpty() && touTiao.getData().get(0) != null) {
                    TouTiao.DataBean dataBean = touTiao.getData().get(0);
                    for (TouTiao.DataBean.WordsBean word : dataBean.getWords()) {
                        HotBean hotBean = new HotBean();
                        hotBean.setTitle(word.getWord());
                        hotBean.setUrl("https://www.toutiao.com/trending/" + word.getId());
                        list.add(hotBean);
                    }
                    touTiaoHotCallBack.onSuccess(list);
                } else {
                    touTiaoHotCallBack.onError(new Exception("头条API路径变了，找插件开发者更新吧~"));
                }
            }

            @Override
            public void onError(Exception e) {
                touTiaoHotCallBack.onError(e);
            }
        });

    }

    public static void main(String[] args) {
//        getToutiaoHotList(new TouTiaoHotCallBack() {
//            @Override
//            public void onSuccess(ArrayList<HotBean> list) {
//                for (HotBean bean : list) {
//                    System.out.println(bean);
//                }
//            }
//
//            @Override
//            public void onError(Exception e) {
//
//            }
//        });

    }
}
